home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / libdemo / options.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  152 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #ifndef __options__
  18. #define __options__
  19.  
  20. #include <stdlib.h>
  21.  
  22. extern char * appName;
  23.  
  24. class option
  25. {
  26. public:
  27.     // API:
  28.     // The constructor declares the option letter and a short description.
  29.     // wasGiven returns true if the option was given on the command line.
  30.     //
  31.     option(int, char *);
  32.     int wasGiven();
  33.  
  34.     // internals:
  35.     virtual int checkOption(int, char *);    // Called from processOptions
  36.                         // class, this performs the
  37.                         // comparison.
  38.     static option * head;            // List of all command line
  39.     option * next;                // options.
  40.     void usage();                // Called when the usage line 
  41.                         // should be printed.
  42. protected:
  43.  
  44.     int opt;
  45.     char * description;
  46.     int given;
  47.  
  48.     static char optString[256];            // Store option chars here for
  49.                         // use in getopt.
  50. };
  51.  
  52.  
  53. class stringOption : public option
  54. {
  55. public:
  56.     // API:
  57.     // Constructor is same as option except and extra optional default.
  58.     // getArgument() returns the option argument given.
  59.     //
  60.     stringOption(int, char *, char * = 0);
  61.     char * getArgument();
  62.  
  63. protected:
  64.     char * arg;
  65.  
  66.     virtual int checkOption(int, char *);    // See if this option 
  67.                         // corresponds to the given
  68.                         // option.  If so, save the
  69.                         // argument.
  70. };
  71.  
  72.  
  73. class manyArgsOption : public stringOption
  74. {
  75. public:
  76.     // Constructor expects option char, description, default string, and 
  77.     // number of comma separated arguments expected in argument.
  78.     // getNumber() gets the number of args gotten. 
  79.     // getArg(n) gets option argument # n.
  80.     //
  81.     manyArgsOption(int, char *, char *, int);
  82.     int getNumber();
  83.     char * getArg(int n);
  84.  
  85. private:
  86.     void parse();                // Do the actuall parsing of 
  87.                         // the arguments.
  88.     char ** args;
  89.     int expected;
  90.     int received;
  91. };
  92.  
  93. class floatOption : public manyArgsOption
  94. {
  95. public:
  96.     // Constructor expects option char, description, default string, and 
  97.     // number of comma separated floats expected in argument.
  98.     // getFloat(n) gets float option argument # n.
  99.     //
  100.     floatOption(int opt, char * desc, char * def, int num)
  101.     : manyArgsOption(opt, desc, def, num) {}
  102.     float getFloat(int n) { return atof(getArg(n)); }
  103. };
  104.  
  105. class intOption : public manyArgsOption
  106. {
  107. public:
  108.     // Constructor expects option char, description, default string, and 
  109.     // number of comma separated ints expected in argument.
  110.     // getInt(n) gets int option argument # n
  111.     //
  112.     intOption(int opt, char * desc, char * def, int num)
  113.     : manyArgsOption(opt, desc, def, num) {}
  114.     int getInt(int n) { return atoi(getArg(n)); }
  115. };
  116.  
  117.  
  118.  
  119. class commandArgument
  120. {
  121. public:
  122.     // Constructor expects a description and the maximum number of arguments.
  123.     // getNumber() gets the number of args gotten. 
  124.     // getArg(n) gets option argument # n.
  125.     //
  126.     commandArgument(char *, int);
  127.     int getNumber();
  128.     char * getArg(int n);
  129.  
  130.     // internal
  131.     int setArgument(int, char **);        // Called from processOptions.
  132.                         // Returns 0 on error.
  133.     void usage();                // Prints usage line for argument.
  134.  
  135.     char * description;
  136.  
  137. private:
  138.     char ** args;
  139.     int expected;
  140.     int received;
  141. };
  142.  
  143.  
  144.  
  145. // User defines this function.  The number of command line arguments and and
  146. // array of pointers to the arguments are passed.  At this point the options
  147. // are already removed.
  148. //
  149. extern void Main();
  150.  
  151. #endif
  152.